package gui.tournamentOptions;

import java.sql.SQLException;
import java.util.LinkedList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.DefaultComboBoxModel;

import core.Constants;
import core.GuiComponentLabels;
import dataBase.DbHolder;

public class TournamentOptionsWindowController implements Observer {

	public TournamentOptionsWindowController() {
		DbHolder.getInstance().getArcherData().addObserver(this);
	}

	public void updateTable() throws SQLException {
		DistancesTableModel model = new DistancesTableModel();
		model.setColumnIdentifiers(new String [] { GuiComponentLabels.ROUND, GuiComponentLabels.DISTANCE });

		String selectedTournamentType = TournamentOptions.getInstance().getTournamentTypeDropDown().getSelectedItem().toString();
		String selectedBowCathegory = TournamentOptions.getInstance().getBowCathegoryDropDown().getSelectedItem().toString();
		String selectedAgeClass = TournamentOptions.getInstance().getAgeClassDropDown().getSelectedItem().toString();
		String selectedGender = TournamentOptions.getInstance().getGenderDropDown().getSelectedItem().toString();

		Integer [] distances = DbHolder.getInstance().getArcherData().getDistances(selectedTournamentType, selectedBowCathegory, selectedAgeClass, selectedGender);

		for (int i = 0; i < distances.length; i++) {
			model.addRow(new Integer [] { i + 1, distances [i] });
		}
		TournamentOptions.getInstance().getDistancesTable().setModel(model);

		/*
		 * if (this.distancesTable.getRowCount() > 0) {
		 * TournamentOptionsWindow.this
		 * .distancesTable.setRowSelectionInterval(this
		 * .distancesTable.getRowCount() - 1, this.distancesTable.getRowCount()
		 * - 1);
		 * TournamentOptionsWindow.this.distancesTable.setColumnSelectionAllowed
		 * (true);
		 * TournamentOptionsWindow.this.distancesTable.setColumnSelectionInterval
		 * (1, 1); }
		 */
	}

	public void setTournamentTypesDropDown() throws SQLException {
		TournamentOptions.getInstance().getTournamentTypeDropDown().setModel(new DefaultComboBoxModel(DbHolder.getInstance().getArcherData().getAllTournamentTypes()));
	}

	public void setBowCathegoryDropDown() {
		TournamentOptions.getInstance().getBowCathegoryDropDown().setModel(new DefaultComboBoxModel(DbHolder.getInstance().getArcherData().getAllBowCathegories()));
	}

	public void setAgeClassesDropDown() {
		TournamentOptions.getInstance().getAgeClassDropDown().setModel(new DefaultComboBoxModel(DbHolder.getInstance().getArcherData().getAllAgeClasses()));
	}

	public void setGenderDropDown() {
		TournamentOptions.getInstance().getGenderDropDown().setModel(new DefaultComboBoxModel(new String [] { Constants.MALE, Constants.FEMALE }));
	}

	public void updateDropDownData() throws SQLException {
		this.setBowCathegoryDropDown();
		this.setAgeClassesDropDown();
		this.setTournamentTypesDropDown();
	}

	public void close() {
		TournamentOptions.getInstance().dispose();
	}

	public void initializeDropDowns() {
		this.setGenderDropDown();
		try {
			this.updateDropDownData();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void addRound() {
		((DistancesTableModel) TournamentOptions.getInstance().getDistancesTable().getModel()).addRow(new Integer [] { TournamentOptions.getInstance().getDistancesTable().getRowCount() + 1, 0 });
	}

	public void removeRound() {
		if (TournamentOptions.getInstance().getDistancesTable().getRowCount() > 1) {
			((DistancesTableModel) TournamentOptions.getInstance().getDistancesTable().getModel()).removeRow(TournamentOptions.getInstance().getDistancesTable().getRowCount() - 1);
		}
	}

	public void saveAction() throws SQLException {

		String [] tournamentTypes;
		String [] bowCathegories;
		String [] ageClasses;
		String [] genders;

		if (TournamentOptions.getInstance().getAllTournamentsCheckBox().isSelected()) {
			tournamentTypes = DbHolder.getInstance().getArcherData().getAllTournamentTypes();
		} else {
			tournamentTypes = new String [] { TournamentOptions.getInstance().getTournamentTypeDropDown().getSelectedItem().toString() };
		}
		if (TournamentOptions.getInstance().getAllBowCathegoriesCheckBox().isSelected()) {
			bowCathegories = DbHolder.getInstance().getArcherData().getAllBowCathegories();
		} else {
			bowCathegories = new String [] { TournamentOptions.getInstance().getBowCathegoryDropDown().getSelectedItem().toString() };
		}
		if (TournamentOptions.getInstance().getAllAgeClassesCheckBox().isSelected()) {
			ageClasses = DbHolder.getInstance().getArcherData().getAllAgeClasses();
		} else {
			ageClasses = new String [] { TournamentOptions.getInstance().getAgeClassDropDown().getSelectedItem().toString() };
		}
		if (TournamentOptions.getInstance().getAllGendersCheckBox().isSelected()) {
			genders = new String [] { Constants.MALE, Constants.FEMALE };
		} else {
			genders = new String [] { TournamentOptions.getInstance().getGenderDropDown().getSelectedItem().toString() };
		}

		LinkedList < Integer > distances = new LinkedList < Integer >();
		for (int i = 0; i < TournamentOptions.getInstance().getDistancesTable().getRowCount(); i++) {
			distances.add(Integer.parseInt(TournamentOptions.getInstance().getDistancesTable().getValueAt(i, 1).toString()));
		}

		for (int i = 0; i < tournamentTypes.length; i++) {
			for (int j = 0; j < bowCathegories.length; j++) {
				for (int k = 0; k < ageClasses.length; k++) {
					for (int l = 0; l < genders.length; l++) {
						DbHolder.getInstance().getArcherData().setDistances(tournamentTypes [i], bowCathegories [j], ageClasses [k], genders [l], distances);
					}
				}
			}
		}
	}

	@Override
	public void update(Observable arg0, Object arg1) {
		try {
			this.updateDropDownData();
			this.updateTable();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
